﻿//订单设置
$(document).ready(function() {

	indentContentView = function(id) {
		$('#redircetWin').window({
			title : "订单详情",
			width : 700,
			height : 500,
			modal : true,
			closed : true,
			shadow : true,
		});
		$('#redircetWin').window('open');
		$('#redircetWin').window('refresh', '/shopping/indent/indentContext?id=' + id);
	}

	$("#indentselect").click(function() {
		$('#tt1').datagrid({
			title : '查询订单信息',
			iconCls : 'icon-save',
			width : 600,
			height : 250,
			nowrap : false,
			striped : true,
			fit : true,
			sortName : 'id',
			sortOrder : 'desc',
			remoteSort : false,
			singleSelect : true,
			fitColumns : true,
			url : "/shopping/admin/indentList",
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 40,
				sortable : true
			}, {
				field : 'userId',
				title : '用户编号',
				width : 80,
			}, {
				field : 'indentDt',
				title : '订单时间',
				width : 150,
				sortable : true,
			}, {
				field : 'totalPrice',
				title : '总价（￥）',
				width : 80,
			}, {
				field : 'status',
				title : '订单状态',
				width : 200,
				formatter : function(val) {
					if (val == 0) {
						return '<span>等待付款</span>';
					}
					if (val == 1) {
						return '<span style="color:red;">等待出库</span>';
					}
					if (val == 2) {
						return '<span style="color:red;">等待收货</span>';
					}
					if (val == 3) {
						return '<span>等待评价</span>';
					}
					if (val == 4) {
						return '<span style="color:grean;">订单已完成</span>';
					}
					if (val == 5) {
						return '<span style="color:red;">用户退货，等待您审核</span>';
					}
					if (val == 6) {
						return '<span style="color:red;">等待物流收到退货</span>';
					}
					if (val == 7) {
						return '<span style="color:red;">退货并把钱退到用户的银行卡里</span>';
					}
					if (val == 8) {
						return '<span style="color:grean;">订单已完成退货</span>';
					}
				}
			}, ] ],
			pagination : true,
			toolbar : [ {
				id : 'btnsearch',
				text : '订单详情',
				iconCls : 'icon-search',
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					if (selected) {
						indentContentView(selected.id);
					} else {
						alert("请选定行！");
					}
				},
			}, '-', {
				id : 'btnremove',
				text : '删除订单',
				iconCls : 'icon-remove',
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					if (selected) {
						var hre = "/shopping/indent/indentDelete?id=" + selected.id;
						if (confirm("确定要删除该订单信息吗？")) {
							$.ajax({
								type : "POST",
								dataType : "json",
								url : hre,
								async : false,
							});
							var row = $('#tt1').datagrid('getSelected');
							if (row) {
								var index = $('#tt1').datagrid('getRowIndex', row);
								$('#tt1').datagrid('deleteRow', index);
							}
						}
						return false;
					} else {
						alert("请选定行！");
					}
				}
			}, '-', {
				id : 'btnfah',
				text : '发货',
				iconCls : 'icon-fah',
				disabled : true,
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					hre = "/shopping/indent/updateIndent?status=2&id=" + selected.id;
					if (confirm("确定要给这个订单发货么？")) {
						$.ajax({
							type : "POST",
							dataType : "json",
							url : hre,
							async : false,
							success : function(data) {
								selected.status = 2;
								var index = $('#tt1').datagrid('getRowIndex', selected);
								$('#tt1').datagrid('refreshRow', index);
								$('#btnfah').linkbutton('disable');
								$('#btnshouh').linkbutton('enable');
							}
						});

					}
					return false;
				},
			}, '-', {
				id : 'btnshouh',
				text : '用户已收货',
				iconCls : 'icon-shouh',
				disabled : true,
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					hre = "/shopping/indent/updateIndent?status=3&id=" + selected.id;
					if (confirm("确定这个订单的客户已收到货了么？")) {
						$.ajax({
							type : "POST",
							dataType : "json",
							url : hre,
							async : false,
							success : function(data) {
								selected.status = 3;
								var index = $('#tt1').datagrid('getRowIndex', selected);
								$('#tt1').datagrid('refreshRow', index);

								$('#btnfah').linkbutton('disable');
								$('#btnshouh').linkbutton('disable');
								$('#btntongguo').linkbutton('enable');
								$('#btnwuliu').linkbutton('disable');
								$('#btntuik').linkbutton('disable');
							}
						});
					}
					return false;
				},
			}, '-', {
				id : 'btntongguo',
				text : '通过审核',
				iconCls : 'icon-tongguo',
				disabled : true,
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					hre = "/shopping/indent/updateIndent?status=6&id=" + selected.id;
					if (confirm("确定这个订单的允许客户退货么？")) {
						$.ajax({
							type : "POST",
							dataType : "json",
							url : hre,
							async : false,
							success : function(data) {
								selected.status = 6;
								var index = $('#tt1').datagrid('getRowIndex', selected);
								$('#tt1').datagrid('refreshRow', index);

								$('#btnfah').linkbutton('disable');
								$('#btnshouh').linkbutton('disable');
								$('#btntongguo').linkbutton('disable');
								$('#btnwuliu').linkbutton('enable');
								$('#btntuik').linkbutton('disable');
							}
						});
					}
					return false;
				},
			}, '-', {
				id : 'btnwuliu',
				text : '收到退货',
				iconCls : 'icon-wuliu',
				disabled : true,
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					hre = "/shopping/indent/updateIndent?status=7&id=" + selected.id;
					if (confirm("确定物流已收到客户的退货么？")) {
						$.ajax({
							type : "POST",
							dataType : "json",
							url : hre,
							async : false,
							success : function(data) {
								selected.status = 7;
								var index = $('#tt1').datagrid('getRowIndex', selected);
								$('#tt1').datagrid('refreshRow', index);
								$('#btnfah').linkbutton('disable');
								$('#btnshouh').linkbutton('disable');
								$('#btntongguo').linkbutton('disable');
								$('#btnwuliu').linkbutton('disable');
								$('#btntuik').linkbutton('enable');
							}
						});
					}
					return false;
				},
			}, '-', {
				id : 'btntuik',
				text : '完成退款',
				iconCls : 'icon-tuik',
				disabled : true,
				handler : function() {
					var selected = $('#tt1').datagrid('getSelected');
					hre = "shopping/indent/updateIndent?status=8&id=" + selected.id;
					if (confirm("确定完成退货并把钱退到用户的银行卡里么？")) {
						$.ajax({
							type : "POST",
							dataType : "json",
							url : hre,
							async : false,
							success : function(data) {
								selected.status = 8;
								var index = $('#tt1').datagrid('getRowIndex', selected);
								$('#tt1').datagrid('refreshRow', index);

								$('#btnfah').linkbutton('disable');
								$('#btnshouh').linkbutton('disable');
								$('#btntongguo').linkbutton('disable');
								$('#btnwuliu').linkbutton('disable');
								$('#btntuik').linkbutton('disable');
							}
						});
					}
					return false;
				},
			} ],
			onClickRow : function(rowIndex, rowData) {
				if (rowData.status == 1) {
					$('#btnfah').linkbutton('enable');
					$('#btnshouh').linkbutton('disable');
					$('#btntongguo').linkbutton('disable');
					$('#btnwuliu').linkbutton('disable');
					$('#btntuik').linkbutton('disable');
				} else if (rowData.status == 2) {
					$('#btnfah').linkbutton('disable');
					$('#btnshouh').linkbutton('enable');
					$('#btntongguo').linkbutton('disable');
					$('#btnwuliu').linkbutton('disable');
					$('#btntuik').linkbutton('disable');
				} else if (rowData.status == 5) {
					$('#btnfah').linkbutton('disable');
					$('#btnshouh').linkbutton('disable');
					$('#btntongguo').linkbutton('enable');
					$('#btnwuliu').linkbutton('disable');
					$('#btntuik').linkbutton('disable');
				} else if (rowData.status == 6) {
					$('#btnfah').linkbutton('disable');
					$('#btnshouh').linkbutton('disable');
					$('#btntongguo').linkbutton('disable');
					$('#btnwuliu').linkbutton('enable');
					$('#btntuik').linkbutton('disable');
				} else if (rowData.status == 7) {
					$('#btnfah').linkbutton('disable');
					$('#btnshouh').linkbutton('disable');
					$('#btntongguo').linkbutton('disable');
					$('#btnwuliu').linkbutton('disable');
					$('#btntuik').linkbutton('enable');
				} else {
					$('#btnfah').linkbutton('disable');
					$('#btnshouh').linkbutton('disable');
					$('#btntongguo').linkbutton('disable');
					$('#btnwuliu').linkbutton('disable');
					$('#btntuik').linkbutton('disable');
				}
			},

		});
	});
});